package com.servlet;

import com.controller.client.RMIClient;
import com.dao.User;
import com.util.MD5;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created by Phillip on 2014/5/7.
 */
public class Validate extends HttpServlet {

    private RMIClient client = new RMIClient();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String username = request.getParameter("username");
        String password_md5 = MD5.getMD5(request.getParameter("password"));

        User user = client.getUser(username);

        if (user.getPassword_afterMD5().equals(password_md5)) {
            System.out.println("in");
            HttpSession session = request.getSession();
            session.setAttribute("isLogon",true);
            session.setAttribute("username", username);
            session.setAttribute("name", user.getName());
            session.setAttribute("credit", user.getCredit());
            session.setAttribute("mobile", user.getPhone());
            session.setAttribute("email", user.getEmail());

            request.setAttribute("isValidated", true);
            response.sendRedirect("/Servlet");
        } else {
            request.setAttribute("isValidated", false);
            request.getRequestDispatcher("index.jsp").forward(request, response);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
